import rclpy
import espeakng
from rclpy.node import Node
from autopatrol_interfaces.srv import SpeakText

class Speaker(Node):
    def __init__(self,node_name):
        super().__init__(node_name)
        self.subscriber = self.create_service(SpeakText,"speech_text",self.CallBack)
        self.speaker = espeakng.Speaker("zh")

    def CallBack(self,request,response):
        self.get_logger().info(f"正在朗读:{request.text}")
        self.speaker.say(request.text)
        self.speaker.wait()
        response.result = True
        return response

def main(args = None):
    rclpy.init(args = args)
    speak_node = Speaker("speaker")
    rclpy.spin(speak_node)
    rclpy.shutdown()